/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gitee.fdc.adminui.demo.interceptor;

import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

/**
 *
 * @author Administrator
 * https://segmentfault.com/a/1190000017393523?utm_source=tag-newest
 */
@Component
@Intercepts({
    //    @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
    //    ,
    @Signature(type = Executor.class, method = "commit", args = {boolean.class})
})
@Slf4j
public class AuditCommitInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 根据签名指定的args顺序获取具体的实现类 
// 1. 获取MappedStatement实例, 并获取当前SQL命令类型
        Boolean ms = (Boolean) invocation.getArgs()[0];
        log.info("AuditCommitInterceptor, is commit : {}", ms);
        return invocation.proceed();

    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
    }

}
